package com.example.echoewish.repository;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.room.Room;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.ChatEntity;
import com.example.echoewish.repository.database.ChatEntityDataBase;
import com.example.echoewish.repository.network.ChatServiceNetWork;
import com.example.echoewish.ui.EchoWishCompatActivity;
import com.example.echoewish.util.EchoWishApplication;
import com.example.echoewish.util.LogUtil;

import java.util.LinkedList;
import java.util.List;

public class ChatMessageRepository {
    ChatServiceNetWork chatServiceNetWork;

    public ChatMessageRepository(int userId,int otherUserId)
    {
        chatServiceNetWork=new ChatServiceNetWork(userId,otherUserId);
    }

    public Boolean checkIsConnect()
    {
        return  chatServiceNetWork.checkIsConnect();
    }

    public void storeInDatabase(ChatEntity chatEntity)
    {
        try {
            ChatEntityDataBase.getDatabase(EchoWishApplication.getContext()).getChatEntityDao().insertChatEntityDao(chatEntity);
            new LogUtil().d(LogUtil.GET_DATA_TAG,"持久化存储聊天记录 成功");
        }
        catch (Exception e)
        {
            new LogUtil().d(LogUtil.GET_DATA_TAG,"持久化存储聊天记录 失败");
        }
    }

    public List<ChatEntity> getChatMessage(int myId, int otherId)
    {
        List<ChatEntity> chatEntities=new LinkedList<>();
        chatEntities.addAll(ChatEntityDataBase.getDatabase(EchoWishApplication.getContext()).getChatEntityDao().getHistoricalRecords(myId,otherId));
        return chatEntities;
    }

    //这里需要实现 手机持久化功能 给数据库里面存储值
    public void sendMessage(String msg)
    {
       try
       {
           chatServiceNetWork.SendMessage(msg);
           new LogUtil().d(LogUtil.GET_DATA_TAG,"发送聊天信息时 发起网络请求成功");
       }
       catch (Exception e)
       {
           new LogUtil().d(LogUtil.GET_DATA_TAG,"发送聊天信息时 发起网络请求失败");
       }
    }

    public void deleteChatMessage()
    {
        try
        {
            ChatEntityDataBase.getDatabase(EchoWishApplication.getContext()).getChatEntityDao().deleteChatEntityDao();
            new LogUtil().d(LogUtil.GET_DATA_TAG,"删除聊天记录成功");
        }
        catch (Exception e)
        {
            new LogUtil().d(LogUtil.GET_DATA_TAG,"删除聊天记录失败");
        }
    }

    public void closeConnect()
    {
        chatServiceNetWork.closeConnect();
    }

}
